import axios from 'axios'
export default {
    namespaced: true,
    state() {
        return {
            goodsList: []
        }
    },
    mutations: {
        setgoodsList(state, list) {
            state.goodsList = list
        },
        getShow(state, val) {
            return state.goodlist.forEach(obj => {
                obj.goods_state = val
            })
        }

    },
    actions: {
        asyncgoodList(store) {
            axios({
                url: "https://www.escook.cn/api/cart",
                method: "GET",
            }).then((res) => {
                console.log(res.data.list);
                store.commit('setgoodsList', res.data.list)
            });
        }
    },
    getters: {
        allCount(state) {

            return state.goodsList
                .filter((obj) => obj.goods_state === true)
                .reduce((sum, obj) => {
                    return (sum += obj.goods_count);
                }, 0);
        },
        allPrice(state) {
            return state.goodsList
                .filter((obj) => obj.goods_state === true)
                .reduce((sum, obj) => {
                    return (sum += obj.goods_count * obj.goods_price);
                }, 0);
        },
        ischecked(state) {
            return state.goodsList.every((obj) => obj.goods_state === true);
        }
    }

}